package com.ruoyi.fpa.service.impl;

import java.util.List;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.util.SnowFlakeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.fpa.domain.FpaFunctionAction;
import com.ruoyi.fpa.mapper.FpaFunctionMapper;
import com.ruoyi.fpa.domain.FpaFunction;
import com.ruoyi.fpa.service.IFpaFunctionService;

/**
 * 功能Service业务层处理
 *
 * @author koradji@163.com
 * @date 2023-09-13
 */
@Service
public class FpaFunctionServiceImpl implements IFpaFunctionService
{
    @Autowired
    private FpaFunctionMapper fpaFunctionMapper;

    /**
     * 查询功能
     *
     * @param id 功能主键
     * @return 功能
     */
    @Override
    public FpaFunction selectFpaFunctionById(String id)
    {
        return fpaFunctionMapper.selectFpaFunctionById(id);
    }

    /**
     * 查询功能列表
     *
     * @param fpaFunction 功能
     * @return 功能
     */
    @Override
    public List<FpaFunction> selectFpaFunctionList(FpaFunction fpaFunction)
    {
        return fpaFunctionMapper.selectFpaFunctionList(fpaFunction);
    }

    /**
     * 新增功能
     *
     * @param fpaFunction 功能
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFpaFunction(FpaFunction fpaFunction)
    {
        if(StringUtils.isEmpty(fpaFunction.getId())) {
            fpaFunction.setId(String.valueOf(SnowFlakeUtils.snowFlakeId()));
        }
        int rows = fpaFunctionMapper.insertFpaFunction(fpaFunction);
        insertFpaFunctionAction(fpaFunction);
        return rows;
    }

    /**
     * 修改功能
     *
     * @param fpaFunction 功能
     * @return 结果
     */
    @Transactional
    @Override
    public int updateFpaFunction(FpaFunction fpaFunction)
    {
        fpaFunctionMapper.deleteFpaFunctionActionByFunctionId(fpaFunction.getId());
        insertFpaFunctionAction(fpaFunction);
        return fpaFunctionMapper.updateFpaFunction(fpaFunction);
    }

    /**
     * 批量删除功能
     *
     * @param ids 需要删除的功能主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFpaFunctionByIds(String[] ids)
    {
        fpaFunctionMapper.deleteFpaFunctionActionByFunctionIds(ids);
        return fpaFunctionMapper.deleteFpaFunctionByIds(ids);
    }

    /**
     * 删除功能信息
     *
     * @param id 功能主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFpaFunctionById(String id)
    {
        fpaFunctionMapper.deleteFpaFunctionActionByFunctionId(id);
        return fpaFunctionMapper.deleteFpaFunctionById(id);
    }

    /**
     * 新增操作信息
     *
     * @param fpaFunction 功能对象
     */
    public void insertFpaFunctionAction(FpaFunction fpaFunction)
    {
        List<FpaFunctionAction> fpaFunctionActionList = fpaFunction.getFpaFunctionActionList();
        String id = fpaFunction.getId();
        if (StringUtils.isNotNull(fpaFunctionActionList))
        {
            List<FpaFunctionAction> list = new ArrayList<FpaFunctionAction>();
            for (FpaFunctionAction fpaFunctionAction : fpaFunctionActionList)
            {
                fpaFunctionAction.setFunctionId(id);
                if(StringUtils.isEmpty(fpaFunctionAction.getId())) {
                    fpaFunctionAction.setId(String.valueOf(SnowFlakeUtils.snowFlakeId()));
                }
                list.add(fpaFunctionAction);
            }
            if (list.size() > 0)
            {
                fpaFunctionMapper.batchFpaFunctionAction(list);
            }
        }
    }
}
